package com.mmr.dxp.mapper;

import com.mmr.dxp.domain.ExchangeDataLog;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface ExchangeDataLogMapper {

    /**
     * 在交换记录备份表中查找未被推送的交换记录
     *
     * @return
     */
    @Select("SELECT SUBID,SENDSUM,RCVSUM,EXCHANGEDATE,OPERATEDATE FROM EXCHANGE_DATE_LOG WHERE exchangeDate =#{lastDay}" +
            " UNION ALL " +
            "SELECT SUBID,SENDSUM,RCVNUM AS RCVSUM,EXCHANGEDATE,OPERATEDATE FROM EXCHANGE_FILE_LOG WHERE exchangeDate =#{lastDay}")
    List<ExchangeDataLog> listExchangeDataLog(@Param("lastDay") String lastDay);

    /**
     * 批量跟新交换日志的推送状态
     *
     * @param dataLogList
     */
    @Update({"<script>\n" +
            "\n" +
            "            update exchange_date_log_blockchain\n" +
            "            <trim prefix=\"set\" suffixOverrides=\",\">\n" +
            "             <trim prefix=\"pushState=case\" suffix=\"end,\">\n" +
            "                 <foreach collection=\"list\" item=\"item\" index=\"index\">\n" +
            "                         <if test=\"item.pushState!=null\">\n" +
            "                          when id=#{item.id} then #{item.pushState}\n" +
            "                         </if>\n" +
            "                 </foreach>\n" +
            "              </trim>\n" +
            "             </trim>\n" +
            "            where\n" +
            "            <foreach collection=\"list\" item=\"item\" index=\"index\">\n" +
            "              id=#{item.id}\n" +
            "          </foreach>\n" +
            "</script>"})
    void updateExchangDataLogBatch(List<ExchangeDataLog> dataLogList);

    /**
     * 根据主键批量删除
     *
     * @param ids
     */
    @Delete({"<script>" +
            "        delete from exchange_date_log_blockchain where id in\n" +
            "        <foreach collection=\"list\" item=\"id\"  separator=\",\" open=\"(\" close=\")\">\n" +
            "            #{id}\n" +
            "        </foreach>\n" +
            "</script>"})
    void batchDelExchangeDataLogByPrimaryKey(List<String> ids);
}
